package study.javaee.practice.controller;

import study.javaee.practice.entity.JpaProject;
import study.javaee.practice.entity.JpaProjectJoinApply;
import study.javaee.practice.result.JpaResult;
import study.javaee.practice.result.JpaResultFactory;
import study.javaee.practice.result.JpaResultcourse;
import study.javaee.practice.service.JpaMd5userservice;
import study.javaee.practice.service.JpaProjectJoinApplyservice;
import study.javaee.practice.service.JpaProjectservice;
import study.javaee.practice.service.JpaUnitservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class projectcontroller {
    @Autowired
    JpaProjectservice ProjService;
    @Autowired
    JpaUnitservice unitservice;
    @Autowired
    JpaMd5userservice md5userservice;
    @Autowired
    JpaProjectJoinApplyservice pjaservice;

    @CrossOrigin({"http://localhost:7777","http://localhost:8080/EstablishProj"})
    @PostMapping(value = "/EstablishProj")
    @ResponseBody
    public JpaResult submitProj(HttpServletRequest request)
    {
        Map<String,String[]> data=request.getParameterMap();
        String ProName= (String) data.get("ProName")[0];
        String Unit=(String) data.get("Unit")[0];
        String username=(String) data.get("username")[0];
        ProName = HtmlUtils.htmlEscape(ProName);
        Unit=HtmlUtils.htmlEscape(Unit);
        username=HtmlUtils.htmlEscape(username);
        System.out.println("{"+ProName+","+Unit+","+username+"}");
        int Uid = unitservice.getbyname(Unit).getId();
        System.out.println(Uid);
        int lid=md5userservice.getByName(username).getId();
        System.out.println(lid);
        Date date=new Date();
        JpaProject p=ProjService.save(new JpaProject(ProName,Uid,lid,date,null,0));
        if(null!=p)
        {
            return new JpaResult(200);
        }
        else {
            return new JpaResult(400);
        }
    }

    @CrossOrigin({"http://localhost:7777","http://localhost:8080/MyProj"})
    @PostMapping(value = "/MyProj")
    @ResponseBody
    public JpaResultcourse getMyProj(HttpServletRequest request){
        Map<String,String[]> data=request.getParameterMap();
        String lname=(String)data.get("username")[0];
        int lid=md5userservice.getByName(lname).getId();
        List<Object> projects=ProjService.getfieldsbyLid(lid);
        return JpaResultFactory.buildSuccessResult(projects);
    }

    @CrossOrigin({"http://localhost:8080/PermittedProj","http://localhost:7777"})
    @GetMapping(value = "/PermittedProj")
    @ResponseBody
    public JpaResultcourse getPermittedProj(){
        List<Object> projects=ProjService.getfieldsbyState(1);
        return JpaResultFactory.buildSuccessResult(projects);
    }

    @CrossOrigin({"http://localhost:7777","http://localhost:8080/PermittedProj"})
    @PostMapping(value = "/PermittedProj")
    @ResponseBody
    public JpaResultcourse saveApply(HttpServletRequest request){
        Map<String,String[]> data=request.getParameterMap();
        String ProName= (String) data.get("ProName")[0];
        String username=(String) data.get("username")[0];
        String content=(String) data.get("content")[0];
        username=HtmlUtils.htmlEscape(username);
        ProName = HtmlUtils.htmlEscape(ProName);
        content=HtmlUtils.htmlEscape(content);
        int Uid = md5userservice.getByName(username).getId();
        int pid=ProjService.getbyname(ProName).getId();
        JpaProjectJoinApply pja=pjaservice.save(new JpaProjectJoinApply(pid,content,Uid,2));
        if(pja!=null)
        {
            return JpaResultFactory.buildSuccessResult(pja);
        }
        else
        {
            return JpaResultFactory.buildFailResult("Error");
        }
    }

    @CrossOrigin({"http://localhost:7777","http://localhost:8080/MyApply"})
    @PostMapping(value = "/MyApply")
    @ResponseBody
    public JpaResultcourse getApply(HttpServletRequest request){
        Map<String,String[]> data=request.getParameterMap();
        String lname=(String)data.get("username")[0];
        int lid=md5userservice.getByName(lname).getId();
        List<Object> projectJoinApplies=pjaservice.getbyUserid(lid);
        return JpaResultFactory.buildSuccessResult(projectJoinApplies);
    }

}
